﻿using System.Linq;
using MetroContractSample.Data;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

// The File Open Picker Contract item template is documented at http://go.microsoft.com/fwlink/?LinkId=234239

namespace MetroContractSample
{
  /// <summary>
  /// This page displays files owned by the application so that the user can grant another application
  /// access to them.
  /// </summary>
  public sealed partial class FileOpenPickerPage : MetroContractSample.Common.LayoutAwarePage
  {
    /// <summary>
    /// Files are added to or removed from the Windows UI to let Windows know what has been selected.
    /// </summary>
    private Windows.Storage.Pickers.Provider.FileOpenPickerUI _fileOpenPickerUI;

    public FileOpenPickerPage()
    {
      this.InitializeComponent();
    }

    /// <summary>
    /// Invoked when another application wants to open files from this application.
    /// </summary>
    /// <param name="args">Activation data used to coordinate the process with Windows.</param>
    public void Activate(FileOpenPickerActivatedEventArgs args)
    {
      this._fileOpenPickerUI = args.FileOpenPickerUI;

      // TODO: Set this.DefaultViewModel["Files"] to show a collection of items,
      //       each of which should have bindable Image, Title, and Description

      var allFiles = App.DataSource.FilteredByExtension(args.FileOpenPickerUI.AllowedFileTypes).ToList();
      allFiles.Sort();
      this.DefaultViewModel["Files"] = allFiles;

      this.DefaultViewModel["CanGoUp"] = false;
      Window.Current.Content = this;
      Window.Current.Activate();

    }

    /// <summary>
    /// Invoked when the selected collection of files changes.
    /// </summary>
    /// <param name="sender">The GridView instance used to display the available files.</param>
    /// <param name="e">Event data that describes how the selection changed.</param>
    private void FileGridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      // TODO: Update Windows UI using this._fileOpenPickerUI.AddFile and
      //       this._fileOpenPickerUI.RemoveFile

      foreach (var item in e.RemovedItems)
        this._fileOpenPickerUI.RemoveFile((item as SampleDataItem).UniqueId);

      foreach (var item in e.AddedItems)
      {
        var dataItem = item as SampleDataItem;
        this._fileOpenPickerUI.AddFile(dataItem.UniqueId, dataItem.FileInfo);
      }
    }

    /// <summary>
    /// Invoked when the "Go up" button is clicked, indicating that the user wants to pop up
    /// a level in the hierarchy of files.
    /// </summary>
    /// <param name="sender">The Button instance used to represent the "Go up" command.</param>
    /// <param name="args">EVent data that describes how the button was clicked.</param>
    private void GoUpButton_Click(object sender, RoutedEventArgs args)
    {
      // TODO: Set this.DefaultViewModel["CanGoUp"] to true to enable the corresponding command,
      //       use updates to this.DefaultViewModel["Files"] to reflect file hierarchy traversal
    }
  }
}
